#include "mytcpserver.h"
#include<QDebug>


MyTcpServer::MyTcpServer()
{


}

MyTcpServer &MyTcpServer::getInstance()
{
    //定义静态的服务器实例
    static MyTcpServer instance;
    return instance;

}

void MyTcpServer::incomingConnection(qintptr socketDescriptor)
{
    qDebug()<<"new tcpclient conected...";

    //产生 MyTcpSocket 对象
    MyTcpSocket *pTcpSocket =new MyTcpSocket;

    pTcpSocket->setSocketDescriptor(socketDescriptor);
    m_tcpSocketList.append(pTcpSocket);

    //如果有 socket 离线
    connect(pTcpSocket,&MyTcpSocket::offline,this,&MyTcpServer::deleteSocket);

}

void MyTcpServer::deleteSocket(MyTcpSocket *mysocket)
{
    //迭代器，查询，遍历后，删除对应的 socket
    QList<MyTcpSocket*>::Iterator iter =m_tcpSocketList.begin();
    for(;iter!=m_tcpSocketList.end();iter++){

        //查询判断
        if(mysocket==*iter)
        {
            //删除 *iter 指针存放的大小
            delete *iter;
            *iter=NULL;
            //删除 m_tcpSocketList List 的 iter 这个元素
            m_tcpSocketList.erase(iter);
            break;
        }
    }
    //打印名称
    for(int i=0;i<m_tcpSocketList.size();i++)
    {
        qDebug()<<m_tcpSocketList.at(i)->getSocketName();
    }

}
